var demo;
var selectData;
$(function() {
    // $('.score').searchableSelect();
    
    if($("#is_add").val()==0){
        checkRole();
    }    
    
    $.ajax({
        url: $('input[name="url"]').val()+'Home/Class_Assess/getSelect',
        type: 'POST',
        dataType: 'json',
        async:false,
        success:function(data){
            if(data.status){
                var t=JSON.parse(data.v);
                selectData=t;
            }
        }
    })

    // class_type = new MobileSelect({
    //   trigger: '#class_type',
    //   title: '请选择课程类型',
    // wheels: [
    //             {data:selectData['class_type']}
    //   ],
    //   position:[0],//初始化定位 打开时默认选中的哪个 如果不填默认为0
    //   callback:function(indexArr, data){
    //         $("#class_type").val(data[0]);
    //         $("#class_type").blur();
    //   }
    // });

    teacher_title = new MobileSelect({
      trigger: '#teacher_title',
      title: '请选择职称',
    wheels: [
                {data:selectData['title']}
      ],
      position:[0],//初始化定位 打开时默认选中的哪个 如果不填默认为0
      callback:function(indexArr, data){
            $("#teacher_title").val(data[0]);
            $("#teacher_title").blur();
      }
    });

    age_groups = new MobileSelect({
      trigger: '#age_groups',
      title: '请选择年龄层次',
    wheels: [
                {data:selectData['age_groups']}
      ],
      position:[0],//初始化定位 打开时默认选中的哪个 如果不填默认为0
      callback:function(indexArr, data){
            $("#age_groups").val(data[0]);
            $("#age_groups").blur();
      }
    });

    teachers_attribute = new MobileSelect({
      trigger: '#teachers_attribute',
      title: '请选择教师属性',
    wheels: [
                {data:selectData['teachers_attribute']}
      ],
      position:[0],//初始化定位 打开时默认选中的哪个 如果不填默认为0
      callback:function(indexArr, data){
            $("#teachers_attribute").val(data[0]);
            $("#teachers_attribute").blur();
      }
    });

    // new Jdate({
    //           el: '#listen_time',
    //           format: 'YYYY-MM-DD hh:mm',
    //           beginYear: 2000,
    //           endYear: 2100
    //         })
           
    $("#goto_step2").click(function(event) {
        resetFields();
        var emptyfieldsCount=new Array();
        var emptyfields = $("#step1 input[value=]");
        console.log(emptyfields);

        $.each(emptyfields, function(k, v) {
            var val=$(this).val();
            console.log(val);
             if(val=='' || val==null){
                emptyfieldsCount.push(v);
             }
        });
       // emptyfieldsCount.shift();
        if (emptyfieldsCount.length > 0) {
            $.each(emptyfieldsCount, function(index, val) {
                if(index==0){
                window.location.hash = '#'+$(this).attr('id');
                console.log(this);
                console.log('#'+$(this).attr('id'));
            }
                 $(this).stop()
                .animate({ left: "-10px" }, 100).animate({ left: "10px" }, 100)

                .animate({ left: "-10px" }, 100).animate({ left: "10px" }, 100)

                .animate({ left: "0px" }, 100)

                .addClass("required");  
                console.log($(this));
               $(this).focus();
            
            });
            return;
        }
        $("#step1").hide();
        $("#step2").show();
        getTop();
        $("#steps").step("next");
    });
    $("#goto_step3").click(function(event) {           
        $("#step2").hide();
        $("#step3").show();
        getTop();
        $("#steps").step("next");
    });
    $("#goto_step4").click(function(event) {
        $("#step3").hide();
        $("#step4").show();
        getTop();
        $("#steps").step("next");
    });
    $("#goto_step5").click(function(event) {
        var none=0;
       

        var teachingReadiness1 = $('input:radio[name="teachingReadiness[0]"]:checked').val();
        var teachingReadiness2 = $('input:radio[name="teachingReadiness[1]"]:checked').val();
        var teachingReadiness3 = $('input:radio[name="teachingReadiness[2]"]:checked').val();
        var teachingReadiness4 = $('input:radio[name="teachingReadiness[3]"]:checked').val();
        var teachingReadiness5 = $('input:radio[name="teachingReadiness[4]"]:checked').val();
        var teachingReadiness6 = $('input:radio[name="teachingReadiness[5]"]:checked').val();
        var teachingReadiness7 = $('input:radio[name="teachingReadiness[6]"]:checked').val();
        var teachingReadiness8 = $('input:radio[name="teachingReadiness[7]"]:checked').val();
        var teachingReadiness9 = $('input:radio[name="teachingReadiness[8]"]:checked').val();
        var teachingReadiness10 = $('input:radio[name="teachingReadiness[9]"]:checked').val();
        var teachingReadiness11 = $('input:radio[name="teachingReadiness[10]"]:checked').val();
        var teachingReadiness12 = $('input:radio[name="teachingReadiness[11]"]:checked').val();
        var teachingReadiness = new Array(teachingReadiness1, teachingReadiness2, teachingReadiness3, teachingReadiness4, teachingReadiness5, teachingReadiness6, teachingReadiness7, teachingReadiness8, teachingReadiness9, teachingReadiness10, teachingReadiness11, teachingReadiness12);
        var none=0;
        $.each(teachingReadiness, function(k, v) {
            if(typeof v=="undefined"){
                $('input[name="teachingReadiness['+k+']"]').parent().css('color', 'red');
                $(this).focus();
                none++;
            }else{
                $('input[name="teachingReadiness['+k+']"]').parent().css('color', 'black');
            }
        });     
        if(none==0){
            $("#step4").hide();
            $("#step5").show();
            $("#steps").step("next");
        }
    });
    $("#goto_step6").click(function(event) {
        // var emptyfields = $("#step5 input[value=]");

        var emptyfieldsCount=new Array();
        var emptyfields = $("#step5 input[value=]");

        $.each(emptyfields, function(k, v) {
            var val=$(this).val();
             if(val=='' || val==null){
                emptyfieldsCount.push(v);
             }
        });


        var classroom_facilities=$('input:radio[name="classroom_facilities"]:checked').val();
        if(typeof classroom_facilities=="undefined"){

            $('input[name="classroom_facilities"]').parent().css('color', 'red');
        }else{
            $('input[name="classroom_facilities"]').parent().css('color', 'black');
        }
        var classroom_environmental=$('input:radio[name="classroom_environmental"]:checked').val();
        if(typeof classroom_environmental=="undefined"){

            $('input[name="classroom_environmental"]').parent().css('color', 'red');
        }else{
            $('input[name="classroom_environmental"]').parent().css('color', 'black');
        }          
        


        resetFields();  
        console.log(emptyfieldsCount);
        if (emptyfieldsCount.length > 0) {
            $.each(emptyfieldsCount, function(index, val) {
                 $(this).stop()
                .animate({ left: "-10px" }, 100).animate({ left: "10px" }, 100)

                .animate({ left: "-10px" }, 100).animate({ left: "10px" }, 100)

                .animate({ left: "0px" }, 100)

                .addClass("required");
                // $(this).css('color','#f00');
                $(this).focus();
            });
            return;
        }
        $("#step5").hide();
        $("#step6").show();
        getTop();
        $("#steps").step("next");
    });
    $("#back_step1").click(function(event) {
        $("#step2").hide();
        $("#step1").show();
        getTop();
        $("#steps").step("previous");
    });
    $("#back_step2").click(function(event) {
        $("#step3").hide();
        $("#step2").show();
        getTop();
        $("#steps").step("previous");
    });
    $("#back_step3").click(function(event) {
        $("#step4").hide();
        $("#step3").show();
        getTop();
        $("#steps").step("previous");
    });
    $("#back_step4").click(function(event) {
        $("#step5").hide();
        $("#step4").show();
        getTop();
        $("#steps").step("previous");
    });
    $("#back_step5").click(function(event) {
        $("#step6").hide();
        $("#step5").show();
        getTop();
        $("#steps").step("previous");
    });
   
    $(".tag_val").on('change','#studentsNorm1',function(event) {   
        var studentsNorm1=$("#studentsNorm1").val();
        var studentsNorm3=$("#studentsNorm3").val();
        if(studentsNorm1!='' && studentsNorm3!=''){
           var studentsNorm4 = (studentsNorm3/studentsNorm1)*100;
            studentsNorm4.toFixed(2);
            studentsNorm4=studentsNorm4;
            $("#studentsNorm4").val(studentsNorm4);
            console.log($("#studentsNorm4").val());            
        }
    });
    $(".tag_val").on('change','#studentsNorm3',function(event) {   
        var studentsNorm1=$("#studentsNorm1").val();
        var studentsNorm3=$("#studentsNorm3").val();
        if(studentsNorm1!='' && studentsNorm3!=''){
           var studentsNorm4 = (studentsNorm3/studentsNorm1)*100;
            studentsNorm4.toFixed(2);
            studentsNorm4=studentsNorm4;
            $("#studentsNorm4").val(studentsNorm4);
            console.log($("#studentsNorm4").val());
        }
    });
     
    if($("#is_add").val()==0){
        var recordcount = 1;
        $("#step6").on('click', '#add_record', function(event) {
            recordcount++;
            $(".content").append("<div><div class='div_border row'><div class='col-xs-2 tag_name ''>时间</div><div class='col-xs-9 tag_val'><input type='text' readOnly class='form-control time_HMS add write'></div></div><div class='div_border' style='height: auto!important;'><div class='div_border row'><div class='col-xs-3 tag_name  ' >教学活动</div><div class='col-xs-9 tag_val '><input type='text' placeholder='请输入教学活动' class='form-control add write' ></div></div><div class='div_border row'><div class='col-xs-3 tag_name  ' >随堂评注</div><div class='col-xs-9 tag_val '><input type='text' placeholder='请输入随堂评注' class='form-control add write' ></div></div></div>");
            var recordid = 3;//原来
            var mydate = new Date();
            var t = mydate.toLocaleString();
            var hour = mydate.getHours();
            var minutes = mydate.getMinutes();
            var second = mydate.getSeconds();
            $(".time_HMS:last").val(hour + ":" + minutes + ":" + second);
            $("#step6_form .add").each(function() {
                // $(this).last().attr("id", "attend_record" + recordid);
                $(this).last().attr("name", "attend_record[" + recordid+"]");
                recordid = recordid + 1;
            });
            
            $("#recordcount").val(recordcount);
        });
        var mydate = new Date();
        var t = mydate.toLocaleString();
        var hour = mydate.getHours();
        var minutes = mydate.getMinutes();
        var second = mydate.getSeconds();
        var myYear = mydate.getFullYear();  // 获取当前年份
        var myMonth = mydate.getMonth() +1; // 获取当前月份
        var myDay = mydate.getDate() // 获取当前日（1- 31）
        $(".time_HMS").val(myYear+'-'+myMonth+'-'+myDay+' '+hour + ":" + minutes + ":" + second);
    }else{
        var recordcount=$(".content").length;
    }
    
    var $tgaUpload = $('#Upload').diyUpload({
        url: 'upload/image',
        success: function(data) {},
        error: function(err) {},
        buttonText: '',
        accept: {
            title: "Images",
            extensions: 'gif,jpg,jpeg,bmp,png'
        },
        thumb: {
            width: 800,
            height: 600,
            quality: 100,
            allowMagnify: true,
            crop: true,
            type: "image/jpeg"
        }
    });


    $("#submit").click(function(event) {
        dialog1 = jqueryAlert({
            'style': 'wap',
            'title': '确认是否提交',
            'content':'提交后无法修改',
            'modal': true,
            'contentTextAlign': 'center',
            'width': '300',
            'height': '150',
            'animateType': 'linear',
            'buttons': {
                '取消':function(){
                   dialog1.close();
               },
                '确定':function(){ 
                    save(2);
                   dialog1.close();
               }
           }
       })      
        
    })
    $("#save").click(function(event) {
        save(1);
        
    })
});

function resetFields() {

$("input[type=text]").removeClass("required");

}

function checkRadio(radioName){
return $("input[name="+radioName+"]:checked").val() == null ? false : true;
}

function getTop() {
    $("body,html").animate({
        scrollTop: 0
    },100);
}

function score(t){
    $(".score_demo").html('');
    score = new selectSwiper({
        el: '.score_demo',
        mustSelect:true, // 是否必选，默认false
        data: selectData['age_groups'],
        init:function(index){},
        okFunUndefind: function() { //选择空执行
            errorAlert('请选择');
            return false;
        },
        okFun: function(index) { //确认按钮执行
            type=this.data[index];
            $(t).html(type);
            
        }
    });   
    score.openSelectSwiper();
}

function save(status){
    var attend1 = $("#attend1").val();
        var attend2 = $("#attend2").val();
        var attend3 = $("#attend3").val();
        var attend4 = $("#attend4").val();
        var attend5 = $("#attend5").val();
        // var attend6 = $("#attend6").val();
        // var attend7 = $("#attend7").val();
        // var attend8 = $("#attend8").val();
        // var attend9 = $("#attend9").val();
        // var attend10 = $("#attend10").val();
        // var attend11 = $("#attend11").val();
        // var attend12 = $("#attend12").val();
        // var attend = new Array(attend1, attend2, attend3, attend4, attend5, attend6, attend7, attend8, attend9, attend10, attend11, attend12);
        var class_id =$("#class_id").val();
        var teacher_id =$("#teacher_id").val();
        var attend= new Array(class_id,teacher_id,attend1,attend2,attend3,attend4,attend5);
        var teachingLevel1= $('#teachingLevel1').val();
        var teachingLevel2 = $("#teachingLevel2").val();
        var teachingLevel3 = $("#teachingLevel3").val();
        var teachingLevel4 = $("#teachingLevel4").val();
        var teachingLevel5 = $("#teachingLevel5").val();
        var teachingLevel6 = $("#teachingLevel6").val();
        var teachingLevel7 = $("#teachingLevel7").val();
        var teachingLevel8 = $("#teachingLevel8").val();
        var teachingLevel9 = $("#teachingLevel9").val();
        var teachingLevel = new Array(teachingLevel1, teachingLevel2, teachingLevel3, teachingLevel4, teachingLevel5, teachingLevel6, teachingLevel7, teachingLevel8, teachingLevel9);
        var teachingEffect1 = $("#teachingEffect1").val();
        var teachingEffect2 = $("#teachingEffect2").val();
        var teachingEffect3 = $("#teachingEffect3").val();
        var teachingEffect4 = $("#teachingEffect4").val();
        var teachingEffect5 = $("#teachingEffect5").val();
        var teachingEffect6 = $("#teachingEffect6").val();
        var teachingEffect = new Array(teachingEffect1, teachingEffect2, teachingEffect3, teachingEffect4, teachingEffect5, teachingEffect6);
        var teachingReadiness1 = $('input:radio[name="item1"]:checked').val();
        var teachingReadiness2 = $('input:radio[name="item2"]:checked').val();
        var teachingReadiness3 = $('input:radio[name="item3"]:checked').val();
        var teachingReadiness4 = $('input:radio[name="item4"]:checked').val();
        var teachingReadiness5 = $('input:radio[name="item5"]:checked').val();
        var teachingReadiness6 = $('input:radio[name="item6"]:checked').val();
        var teachingReadiness7 = $('input:radio[name="item7"]:checked').val();
        var teachingReadiness8 = $('input:radio[name="item8"]:checked').val();
        var teachingReadiness9 = $('input:radio[name="item9"]:checked').val();
        var teachingReadiness10 = $('input:radio[name="item10"]:checked').val();
        var teachingReadiness11 = $('input:radio[name="item11"]:checked').val();
        var teachingReadiness12 = $('input:radio[name="item12"]:checked').val();
        var teachingReadiness = new Array(teachingReadiness1, teachingReadiness2, teachingReadiness3, teachingReadiness4, teachingReadiness5, teachingReadiness6, teachingReadiness7, teachingReadiness8, teachingReadiness9, teachingReadiness10, teachingReadiness11, teachingReadiness12);
        var studentsNorm1 = $("#studentsNorm1").val();
        var studentsNorm2 = $("#studentsNorm2").val();
        var studentsNorm3 = $("#studentsNorm3").val();
        var studentsNorm4 = (studentsNorm3/studentsNorm1)*100;
        studentsNorm4.toFixed(2);
        studentsNorm4=studentsNorm4+'%';
        $("#studentsNorm4").val(studentsNorm4);        
        var studentsNorm5 = $("#studentsNorm5").val();
        var studentsNorm6 = $("#studentsNorm6").val();
        var studentsNorm7 = $("#studentsNorm7").val();
        var studentsNorm8 = $("#studentsNorm8").val();
        var studentsNorm9 = $("#studentsNorm9").val();
        var studentsNorm = new Array(studentsNorm1, studentsNorm2, studentsNorm3, studentsNorm4, studentsNorm5, studentsNorm6, studentsNorm7, studentsNorm8, studentsNorm9);
        var classroom_facilities = $('input:radio[name="item13"]:checked').val();
        var classroom_environmental = $('input:radio[name="item14"]:checked').val();
        var attend_record = new Array();
        $("#step6_form .write").each(function() {
            attend_record.push($(this).val());
        });
        $("#attend_record").val(attend_record);
        var is_add=$("#is_add").val();

        
        var k=0;
        $("#img").html('');
        $("li img").each(function(k,v) {
            console.log(this);
            $("#img").append("<input type='hidden' name='attend_img["+k+"]' class='attend_img' value='"+$(this).attr('src')+"'>");
            k++;
        });
        
        var attend_id=$('#is_add').val()
        if(attend_id==0){
            var url='Home/Class_Assess/saveAttend';
        }else{
           var url='Home/Class_Assess/saveAttend/'+attend_id;
       }
       console.log($("#img .attend_img").val());
       var data=$("form").serialize();
       data=data+"&save="+status;

       

    $.ajax({
        url: $('input[name="url"]').val() + url,
        type: 'POST',
        dataType: 'json',
      
        data:data,
        success: function(data) {
            var t=JSON.parse(data.v);
            if (data.status) {
                var dialog1 = jqueryAlert({
                    'icon': $('input[name="url"]').val() + 'public/home/alert/img/right.png',
                    'content': data.msg,
                    'closeTime': 1000,
                })
                $("#submit").attr('disabled',true);
                $("#save").attr('disabled',true);
                setTimeout(window.location.href = $('input[name="url"]').val()+"Home/Index",2000);
            } else {
                var dialog1 = jqueryAlert({
                    'icon': $('input[name="url"]').val() + 'public/home/alert/img/error.png',
                    'content': data.msg,
                    'closeTime': 1000,
                })
                if(t['error']==1){
                    for(var i=1;i<=6;i++){
                        $("#step"+i).hide();
                    }
                    $("#steps").step("goto", 2);
                    $("#step2").show();
                }else if(t['error']==2){
                    for(var i=1;i<=6;i++){
                        $("#step"+i).hide();
                    }
                    $("#steps").step("goto", 3);
                    $("#step3").show();
                }else if(t['error']==3){
                   $(".alert-container").css('display','block');
                   $(".alert-modal").css('display','block');
                }
                $("#submit").attr('disabled',true);
            }
        }
    })
}


function checkRole(){
    $.ajax({
            url: $('input[name="url"]').val()+'Home/Class_Assess/checkSubrole',
            type: 'POST',
            dataType: 'json',
            async:false,
            success:function(data){
                if(data.status){
                    var t=JSON.parse(data.v);
                    if(t['html']){
                        dialog1 = jqueryAlert({
                            'style': 'wap',
                            'title': '请选择提交反馈身份',
                            'content': t['html'],
                            'modal': true,
                            'contentTextAlign': 'center',
                            'width': '300',
                            'height': '120',
                            'animateType': 'linear',
                            'buttons': {
                               
                                '确定':function(){
                                   if($("#sub_role").val()=='0'){
                                        var dialog2 = jqueryAlert({
                                        'icon': $('input[name="url"]').val() + 'public/home/alert/img/error.png',
                                        'content': '请选择反馈身份！',
                                        'closeTime': 1000,
                                    })
                                   }else{
                                        dialog1.close();
                                        $('input[name="sub_role"]').val($("#sub_role").val());
                                   }
                                   
                               }
                           }
                       })
                    }
                }
            }
        })
}

// 实时计算打分情况
function levelsum(){
    var sum=0;
    $.each($("#step2 .margin-bottom input[type='text']"), function(k, v) {
        // console.log(v);
        var id=this.id;
        var val=$('#'+id).val();
        if(val!='' ){
            val=parseInt(val);
            sum+=val;
        }            
    });
    $(".teaching_level_sum").html('当前总分为：'+sum);
}
function effectsum(){
    var sum=0;
    $.each($("#step3 .margin-bottom input[type='text']"), function(k, v) {
        // console.log(v);
        var id=this.id;
        var val=$('#'+id).val();
        if(val!='' ){
            val=parseInt(val);
            sum+=val;
        }            
    });
    $(".teaching_effect_sum").html('当前总分为：'+sum);
}